package com.myapp.tools.dbbuilder;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.myapp.tools.bean.ToolsConfig;
import com.myapp.tools.dbbuilder.base.TableBean;
import com.myapp.tools.util.BuilderTools;

public class DBBuilder {

	public void builderDB(TableBean tableBean) {
		this.builderEntity(tableBean);
		this.builderBaseDao(tableBean);
		this.builderDao(tableBean);
	}

	private void builderDao(TableBean tableBean) {
		String template = ToolsConfig.appToolsConfigBean.getDao_Template();
		String templatePath = ToolsConfig.appToolsConfigBean.getBase_Path()
				+ File.separator + template;

		String packagePath = ToolsConfig.appToolsConfigBean.getDao_Package()
				+ ".manual";

		String savePath = ToolsConfig.appToolsConfigBean.getBase_Path()
				+ File.separator + "export" + File.separator + "src"
				+ File.separator
				+ packagePath.replaceAll("\\.", "\\" + File.separator);

		String tmp = (tableBean.getName().substring(0, 1)).toUpperCase()
				+ tableBean.getName().substring(1);

		String fileName = tmp + "Dao.java";

		// 准备数据
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("ConfigPackage",
				ToolsConfig.appToolsConfigBean.getDao_Package());
		root.put("Name", tableBean.getName());

		// 生成文件
		BuilderTools.build(templatePath, savePath, fileName, root);
	}

	private void builderBaseDao(TableBean tableBean) {
		String template = ToolsConfig.appToolsConfigBean.getBaseDao_Template();
		String templatePath = ToolsConfig.appToolsConfigBean.getBase_Path()
				+ File.separator + template;

		String packagePath = ToolsConfig.appToolsConfigBean.getDao_Package()
				+ ".base";

		String savePath = ToolsConfig.appToolsConfigBean.getBase_Path()
				+ File.separator + "export" + File.separator + "src"
				+ File.separator
				+ packagePath.replaceAll("\\.", "\\" + File.separator);

		String tmp = (tableBean.getName().substring(0, 1)).toUpperCase()
				+ tableBean.getName().substring(1);

		String fileName = "Base" + tmp + "Dao.java";

		// 准备数据
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("ConfigPackage", packagePath);
		root.put("Name", tableBean.getName());
		root.put("FieldBeans", tableBean.getFields());
		if (tableBean.getIndexs() != null) {
			root.put("Indexs", tableBean.getIndexs());
		}

		if (tableBean.getPrimayKey() != null) {
			root.put("PrimayKey", tableBean.getPrimayKey());
		}
		
		// 生成文件
		BuilderTools.build(templatePath, savePath, fileName, root);
	}

	private void builderEntity(TableBean tableBean) {
		String template = ToolsConfig.appToolsConfigBean.getEntity_Template();
		String templatePath = ToolsConfig.appToolsConfigBean.getBase_Path()
				+ File.separator + template;

		String packagePath = ToolsConfig.appToolsConfigBean.getEntity_Package();

		String savePath = ToolsConfig.appToolsConfigBean.getBase_Path()
				+ File.separator + "export" + File.separator + "src"
				+ File.separator
				+ packagePath.replaceAll("\\.", "\\" + File.separator);

		String tmp = (tableBean.getName().substring(0, 1)).toUpperCase()
				+ tableBean.getName().substring(1);

		String fileName = tmp + "Entity.java";

		// 准备数据
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("ConfigPackage", packagePath);
		root.put("Name", tableBean.getName());
		root.put("FieldBeans", tableBean.getFields());
		root.put("Remark", tableBean.getRemark());

		// 生成文件
		BuilderTools.build(templatePath, savePath, fileName, root);
	}
}
